﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Net;

namespace rmeloni.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult About(FormCollection collection)
        {
            ViewData["vnome"] = Request.Params["NomeCompleto"];
            ViewData["vemail"] = Request.Params["Email"];
            ViewData["vassunto"] = Request.Params["Assunto"];
            ViewData["vempresa"] = Request.Params["Empresa"];
            ViewData["vmensagem"] = Request.Params["Mensagem"];

            if (String.IsNullOrEmpty(Request.Params["NomeCompleto"]))
                ModelState.AddModelError("C_NomeCompleto", "Preencha o campo Nome Completo.");

            if (String.IsNullOrEmpty(Request.Params["Email"]))
                ModelState.AddModelError("C_Email", "Preencha o campo Email.");

            if (String.IsNullOrEmpty(Request.Params["Mensagem"]))
                ModelState.AddModelError("C_Mensagem", "Preencha o campo Mensagem.");

            if (ModelState.Count > 0)
                return View();

            try
            {
                SmtpClient cliente = new SmtpClient("smtp.gmail.com", 587);
                cliente.EnableSsl = true;
                
                System.Security.SecureString senha = new System.Security.SecureString();
                cliente.Credentials = new NetworkCredential("raphael.meloni@gmail.com", senha);

                MailMessage email = new MailMessage();
                email.From = new MailAddress(Request.Params["Email"], Request.Params["NomeCompleto"]);

                email.To.Add("raphael.meloni@gmail.com");

                email.Subject = Request.Params["Assunto"];

                email.Priority = MailPriority.High;

                email.Sender = email.From;

                email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

                email.BodyEncoding = System.Text.Encoding.Default;

                email.To.Add(Request.Params["Email"]);

                email.Body = Request.Params["Mensagem"];

                cliente.Send(email);

                ModelState.AddModelError("msgreturn", "Mensagem enviada com sucesso.");

                ViewData["vnome"] = "";
                ViewData["vemail"] = "";
                ViewData["vassunto"] = "";
                ViewData["vempresa"] = "";
                ViewData["vmensagem"] = "";
            }
            catch(System.Exception)
            {
                ModelState.AddModelError("msgreturn", "Erro no envio. Verifique seu email.");
                return View();
            }
            
            return View();
        }

    }
}
